package com.trkj.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.trkj.entity.Member;
import com.trkj.utils.MD5;

/**
 * 会员DAO 测试测试测试
 * 会员DAO 雷纯的……
 * @author Administrator
 *
 */
public class MemberDAO extends BaseDAO {
	private Member resultSet2Member(ResultSet rs) throws SQLException {
		Member member = new Member();
		member.setMid(rs.getInt(1));
		member.setLoginName(rs.getString(2));
		member.setRealName(rs.getString(3));
		member.setPassword(rs.getString(4));
		member.setRegTime(rs.getTimestamp(5));
		member.setScore(rs.getInt(6));
		member.setHeaderImage(rs.getString(7));
		member.setPhone(rs.getString(8));
		member.setSex(rs.getString(9));
		member.setBirthday(rs.getDate(10));
		return member;
	}
	
	/**
	 * 登陆 
	 * @param loginName 登陆名
	 * @param password 密码
	 * @return
	 */
	public Member login(String loginName, String password){
		String sql = "select * from member "
				+ "where login_name=? and password=?";
		Member member = get(sql, new RowMapper<Member>() {
			@Override
			public Member mapper(ResultSet rs) throws SQLException {
				return resultSet2Member(rs);
			}
		}, loginName, MD5.encoderByMd5(password));
		return member;
	}
	
	
	/**
	 * 极简注册
	 * @param loginName
	 * @param password
	 */
	public boolean register(String loginName, String password){
		//检验用户名是否存在
		String sql1 = "select count(*) "
				+ "from member where login_name=?";
		Integer count = get(sql1, new RowMapper<Integer>() {
			@Override
			public Integer mapper(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, loginName);
		//判断count是否为0，如果为0表示该用户名没有注册，可以使用
		if(count == 0){
			String sql = "insert into member("
					+ "login_name, password, reg_time)"
					+ " values(?,?,?)";
			executeUpdate(sql, loginName, 
					MD5.encoderByMd5(password), new Date());
			return true;
		}
		return false;
	}
	
	/**
	 * 分页查询会员
	 * @param curpage 当前页
	 * @param pagesize 页大小
	 * @return
	 */
	public List<Member> queryByPage(int curpage, int pagesize){
		String sql = "select * from member "
				+ "order by mid desc limit ?,?";
		int start = (curpage - 1) * pagesize;
		List<Member> list = executeQuery(sql, new RowMapper<Member>() {
			@Override
			public Member mapper(ResultSet rs) throws SQLException {
				return resultSet2Member(rs);
			}
		}, start, pagesize);
		return list;
	}
	
	/**
	 * 计算有多少个会员
	 * @return
	 */
	public int count(){
		String sql = "select count(*) from member";
		return get(sql, new RowMapper<Integer>() {
			@Override
			public Integer mapper(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		});
	}
	
	/**
	 * 根据编号获取会员对象
	 * @param mid
	 * @return
	 */
	public Member get(int mid){
		String sql = "select * from member where mid=?";
		return get(sql, new RowMapper<Member>() {
			@Override
			public Member mapper(ResultSet rs) throws SQLException {
				return resultSet2Member(rs);
			}
		}, mid);
	}
	
	/**
	 * 编辑会员
	 * @param mid
	 * @param realName
	 * @param sex
	 * @param phone
	 * @param birthday
	 */
	public void edit(int mid, String realName, 
			String sex, String phone, Date birthday){
		String sql = "update member set real_name=?,"
				+ " sex=?, phone=?, birthday=? "
				+ "where mid=?";
		executeUpdate(sql, realName, sex, phone,
				birthday, mid);
	}
	
	/**
	 * 重置密码
	 * @param mid
	 */
	public void resetPassword(int mid){
		String sql = "update member set password=?"
				+ " where mid=?";
		executeUpdate(sql, MD5.encoderByMd5("aaaaaa"), mid);
	}
}










